﻿using System.Collections.Generic;
using Ovvio.Business;
using Ovvio.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Ovvio.Web.Core;
using System;
using System.Runtime.Serialization;
using System.Web.Mvc;
using System.Linq;
using Ovvio.Web.Models;


namespace Ovvio.Web.Areas.Admin.Models
{

   

    [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
    public class UserModel : BaseModel<User>
    {
        private string _password;
        private string _confirmPassword;

        public UserModel()
        {
            this.IsActive = true;
            this.IsNewsLetterSubscriber = true;
            this.Role = Role.Guest;
        }

        [ScaffoldColumn(false)]
        public int Id
        {
            get { return Entity.Id; }
            set { Entity.Id = value; }
        }
      
        [Required]
        [DisplayName("Username")]
        public string Username 
        { 
            get { return Entity.Username; } 
            set { Entity.Username = value;}
        }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Confirm Password")]
        public string ConfirmPassword
        {
            get { return _confirmPassword; }
            set { _confirmPassword = value; }
        }

        [Required]
        [DisplayName("First Name")]
        public string FirstName
        {
            get {return Entity.FirstName; }
            set { Entity.FirstName = value; }
        }

        [Required]
        [DisplayName("Last Name")]
        public string LastName
        {
            get {return Entity.LastName; }
            set {Entity.LastName = value; }
        }

        [Required]
        [DisplayName("Mobile")]
        public string Mobile
        { 
            get { return Entity.Mobile; }
            set {Entity.Mobile = value; }
        }

        [Required]
        [DisplayName("Email")]
        [DataType(DataType.EmailAddress)]
        public string Email
        { 
            get { return Entity.Email; } 
            set {Entity.Email = value; }
        }

        [DisplayName("Active")]
        public bool IsActive
        {
            get { return Entity.IsActive; } 
            set {Entity.IsActive = value; }
        }
        
        [UIHint("RoleUserControl")]
        public Role Role
        {
            get { return (Role) Entity.Role; }
            set { Entity.Role = (short)value; }
        }
        
        [DisplayName("Subscriber")]
        public bool IsNewsLetterSubscriber
        {
            get { return Entity.IsNewsLetterSubscriber; } 
            set {Entity.IsNewsLetterSubscriber = value;}
        }

        [ScaffoldColumn(false)]
        [DisplayName("Last Updated By")]
        public string UpdatedBy
        {
            get { return Entity.UpdatedBy; }
        }

        [ScaffoldColumn(false)]
        [DisplayName("Last Login")]
        public string LastLoginDate
        {
            get { return Entity.LastLogonDate.HasValue ? Entity.LastLogonDate.Value.ToLongTimeString() : string.Empty; }
        }

        protected override void OnEntityChanged(User entity)
        {
            _password = entity.DecryptPassword();
            _confirmPassword = _password;
        }

        public static SelectList GetRoles(Role selectedRole) 
        {

            var values = from Role e in Enum.GetValues(typeof(Role))
                         select new { ID = (int)e, Name = e.ToString() };

            return new SelectList(values, "ID", "Name", (int)selectedRole);
        }
    }
}
